package com.haiyi.software.app;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ActivityModifyBirthday extends Activity {

	private Button btn_saveback;

	private String birthday;

	private WheelView wheel_year;

	private WheelView wheel_month;

	private WheelView wheel_day;

	private final static int MAXAGE = 102;

	Calendar calendar = Calendar.getInstance();

	private int year = 0;

	private int month = 0;

	private int day = 0;

	private List<Integer> yearList = new ArrayList<Integer>();

	private int yearIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_modify_birthday);

		getParameters();

		initComp();

		initData();
	}

	private void initData() {

		initYear();

		wheel_day.setCyclic(true);
		wheel_month.setCyclic(true);
		wheel_year.setCyclic(true);
		// month
		int curMonth = calendar.get(Calendar.MONTH);
		String months[] = new String[] { "1月", "2月", "3月", "4月", "5月", "6月",
				"7月", "8月", "9月", "10月", "11月", "12月" };
		wheel_month
				.setViewAdapter(new DateArrayAdapter(this, months, curMonth));
		wheel_month.setCurrentItem(this.month - 1);
		wheel_month.addChangingListener(listener);

		// year
		int curYear = calendar.get(Calendar.YEAR) - 18;

		Log.i("year", curYear + "");

		wheel_year.setViewAdapter(new DateNumericAdapter(this,
				curYear - MAXAGE, curYear, 0));
		wheel_year.setCurrentItem(this.yearIndex);
		wheel_year.addChangingListener(listener);

		// day
		updateDays(wheel_year, wheel_month, wheel_day);
		wheel_day.setCurrentItem(this.day - 1);
	}

	private void initYear() {
		// year
		int curYear = calendar.get(Calendar.YEAR) - 18;

		for (int i = curYear - 102; i <= curYear; i++) {

			yearList.add(i);

		}

		yearIndex = yearList.indexOf(this.year);

	}

	private void initComp() {
		// TODO 自动生成的方法存根
		this.btn_saveback = (Button) this.findViewById(R.id.btn_saveback);
		this.wheel_year = (WheelView) this.findViewById(R.id.wheel_year);
		this.wheel_month = (WheelView) this.findViewById(R.id.wheel_month);
		this.wheel_day = (WheelView) this.findViewById(R.id.wheel_day);
		this.btn_saveback.setOnClickListener(saveClick);
	}

	private void getParameters() {

		this.birthday = this.getIntent().getStringExtra("birthday");

		Log.i("birthday", birthday);

		Log.i("birthday", birthday);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date dt;
		try {
			dt = sdf.parse(birthday);
			Calendar calendar = Calendar.getInstance();

			calendar.setTime(dt);

			this.year = calendar.get(Calendar.YEAR);
			this.month = calendar.get(Calendar.MONTH) + 1;
			this.day = calendar.get(Calendar.DAY_OF_MONTH);

			Log.e("y-m-d", year + ":" + month + ":" + day);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			Log.e("日期转换失败", e.getMessage());
		}

	}

	private OnClickListener saveClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			year = yearList.get(wheel_year.getCurrentItem());

			month = wheel_month.getCurrentItem() + 1;

			day = wheel_day.getCurrentItem() + 1;

			Intent intent = new Intent();

			intent.putExtra("birthday", year + "-" + month + "-" + day);

			setResult(ActivityModifyInformation.SHOW_SUB_ACTIVITY_BIRTHDAY,
					intent);

			ActivityModifyBirthday.this.finish();
		}
	};

	OnWheelChangedListener listener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			updateDays(wheel_year, wheel_month, wheel_day);
		}
	};

	/**
	 * Updates day wheel. Sets max days according to selected month and year
	 */
	void updateDays(WheelView year, WheelView month, WheelView day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, yearList.get(year.getCurrentItem()));
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, 0));
		int curDay = Math.min(maxDays, this.day);
		day.setCurrentItem(curDay - 1, true);
	}

	/**
	 * Adapter for numeric wheels. Highlights the current value.
	 */
	private class DateNumericAdapter extends NumericWheelAdapter {
		// Index of current item
		int currentItem;
		// Index of item to be highlighted
		int currentValue;

		/**
		 * Constructor
		 */
		public DateNumericAdapter(Context context, int minValue, int maxValue,
				int current) {
			super(context, minValue, maxValue);
			// this.currentValue = current;
			setTextSize(20);

		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			// if (currentItem == currentValue) {
			// view.setTextColor(0xFF0000F0);
			// }
			view.setTypeface(Typeface.SANS_SERIF);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}

	/**
	 * Adapter for string based wheel. Highlights the current value.
	 */
	private class DateArrayAdapter extends ArrayWheelAdapter<String> {
		// Index of current item
		int currentItem;
		// Index of item to be highlighted
		int currentValue;

		/**
		 * Constructor
		 */
		public DateArrayAdapter(Context context, String[] items, int current) {
			super(context, items);
			this.currentValue = current;
			setTextSize(20);

		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			// if (currentItem == currentValue) {
			// view.setTextColor(0xFF0000F0);
			// }
			view.setTypeface(Typeface.SANS_SERIF);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}
}
